//index.js
//获取应用实例
import showDetail from "../../modules/showDetail";
import showcDetail from "../../modules/showcDetail";
const app = getApp();
Page({
  data: {
    index_slides: [],
    indicator_dots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    nav_data: [],
    index_activity: [],
    index_block: [],
    isTap: false,
    isLoading: false,
    sectionListRequest: [],
    indexNav: [],
    pageNum: 1,
    keyword: 'all',
    totalPages: 5,
    slidegood: [],
    Authorization: '',
  },
  onLoad() {
    var that = this;
    this.setData({
      Authorization: wx.getStorageSync('Authorization'),
    })
    console.info("testmessag is: "+wx.getStorageSync('Authorization'));
    wx.request({
      url: "http://localhost:8080/wechat/selectindexgoods",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'pageNum': that.data.pageNum,
      },
      success: function (res) {
        console.info(res.statusCode);
        if (res.statusCode == 200) {
          that.setData({
            index_block: res.data['section'],
            totalPages: res.data['totalPages'],
          })
          console.info(res);
          console.info(that.data.index_block);
          console.info("totalPages is " + res.data['totalPages']);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
    })
    wx.request({
      url: "http://localhost:8080/wechat/slidead",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      success: function (res) {
        console.info(res.statusCode);
        if (res.statusCode == 200) {
          that.setData({
            index_slides: res.data,
          })
          console.log(res);
          console.log(that.data.index_slides);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
    })
    wx.request({
      url: "http://localhost:8080/wechat/selectindexnav",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      success: function (res) {
        console.info(res.statusCode);
        if (res.statusCode == 200) {
          that.setData({
            indexNav: res.data,
          })
          console.info(res);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
    })
    const
      nav_data = app.globalData.nav_data,
      index_activity = app.globalData.index_activity,
      index_block = app.globalData.index_block
    // sectionList = index_block.map((section) => {
    //   return section.section;
    // })
    console.info(this.data.pageNum);
    this.setData({
      nav_data,
      index_activity,
      index_block,
    });

  },
  onShow(e) {
    this.setData({
      isTap: false
    });
  },
  //事件处理函数
  toSearch(e) {
    this.setData({
      isTap: true
    });
    wx.navigateTo({
      url: "../search/search"
    })
  },
  onPullDownRefresh() {
    console.log('--------下拉刷新-------')
    var that = this;
    wx.showLoading({
      title: '请稍等',
    });
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.request({
      url: "http://localhost:8080/wechat/selectindexgoods",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'pageNum': this.data.pageNum + 1,
      },
      success: (res) => {
        if (res.statusCode == 200) {
          that.setData({
            pageNum: this.data.pageNum + 1,
            index_block: this.data.index_block.concat(res.data['section']),
          })
          wx.hideLoading();
          console.info(this.data.sectionListRequest);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        wx.hideLoading();
        // fail
      },
      complete: () => {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
  onReachBottom() {
    console.log("向上滑动")
    //判断是否还有下一条数据
    if (this.data.pageNum >= this.data.totalPages) {
      //表示没有下一页数据
      //console.log("没有下一页数据")
      wx.showToast({
        title: '已经到底了'
      })
    } else {
      console.info(this.data.pageNum);
      console.info(this.data.totalPages);
      var Authorization=wx.getStorageSync('Authorization')
      if(Authorization==''){ 
        wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
      }else{
        this.onPullDownRefresh();
      }
    }
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  //回到顶部
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // showDetail(e){
  //   const id=e.currentTarget.dataset.pid;
  //   console.log(id);
  // }
  showDetail,
  showcDetail
})